﻿<template>
	<div class="mrsPackageUser-container">
		<el-dialog v-model="state.isShowDialog" draggable width="769px">
			<template #header>
				<div style="color: #fff">
					<el-icon size="16" style="margin-right: 3px; display: inline; vertical-align: middle"> <ele-Edit />
					</el-icon>
					<span> {{ props.title }} </span>
				</div>
			</template>
			<el-form :model="state.ruleForm" ref="ruleFormRef" size="default" label-width="80px" :rules="rules">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="产品包" prop="packageId">
							<el-select v-model.trim="state.ruleForm.packageId" placeholder="请选择" class="w100">
								<el-option :label="item.label" :value="item.value"
									v-for="item, index in state.SysMrsPackageList" :key="index"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<!-- <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="包代码" prop="packageCode">
							<el-input v-model="state.ruleForm.packageCode" placeholder="请输入产品包代码" class="w100" clearable />
						</el-form-item>
					</el-col> -->
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="用户" prop="userId">
							<el-select v-model.trim="state.ruleForm.userId" placeholder="请选择" class="w100">
								<el-option :label="item.label" :value="item.value"
									v-for="item, index in state.SysMrsUserApiList" :key="index"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="手机号" prop="phone">
							<el-input v-model="state.ruleForm.phone" placeholder="请输入手机号" class="w100" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="期限" prop="expireTime">
							<!-- <el-input v-model="state.ruleForm.expireTime" placeholder="请输入期限" class="w100" clearable /> -->
							<el-date-picker v-model.trim="state.ruleForm.expireTime" type="datetime" placeholder="选择日期"
								value-format="YYYY-MM-DD" style="width: 100%" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="状态" prop="status">
							<el-radio-group v-model="state.ruleForm.status" class="ml-4">
								<el-radio :label="1">启用</el-radio>
								<el-radio :label="2">停用</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="cancel" size="default">取 消</el-button>
					<el-button type="primary" @click="submit" size="default">确 定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts" setup name="editMrsPackageUser" >
import { reactive, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import type { FormRules } from "element-plus";
import mittBus from '/@/utils/mitt';
import SysMrsPackageApi from '/@/api/memberRights/package';
import { MrsPackageUser } from '/@/api/models';
import SysMrsPackageUserApi from '/@/api/memberRights/packageUser';
import SysMrsUserApi from '/@/api/memberRights/user';

// 定义变量内容
const props = defineProps({
	title: String,
});
const ruleFormRef = ref();
const state = reactive({
	isShowDialog: false,
	ruleForm: {} as MrsPackageUser,
	SysMrsPackageList: [] as any,
	SysMrsUserApiList: [] as any,
});

// 删除非必填规则
const rules = ref<FormRules>({
	packageId: [{ required: true, message: '请输入产品包ID！', trigger: 'blur', },],
	packageCode: [{ required: true, message: '请输入产品包代码！', trigger: 'blur', },],
	phone: [{ required: true, message: '请输入手机号！', trigger: 'blur', },],
	userId: [{ required: true, message: '请输入用户ID！', trigger: 'blur', },],
});

// 打开弹窗
const openDialog = async (row: MrsPackageUser) => {
	state.ruleForm = row;
	state.isShowDialog = true;
	await resSysMrsPackageApi();
	await resSysMrsUserApi();
};

// 关闭弹窗
const closeDialog = () => {
	mittBus.emit('submitRefresh');
	state.isShowDialog = false;
};

// 取消
const cancel = () => {
	state.isShowDialog = false;
};

// 提交
const submit = () => {
	ruleFormRef.value.validate(async (valid: boolean, fields?: any) => {
		if (valid) {
			let res;
			if (state.ruleForm.id != undefined && state.ruleForm.id > 0)
				res = await SysMrsPackageUserApi().editPost(state.ruleForm);
			else
				state.SysMrsPackageList.find((item: any) => item.value == state.ruleForm.packageId ? state.ruleForm.packageCode = item.exValue : ''
				)
			res = await SysMrsPackageUserApi().addPost(state.ruleForm);
			if (res.data.resCode != '0000')
				ElMessage.error(res.data.resMessage);
			else {
				ElMessage.success("操作成功");
				closeDialog();
			}
		} else {
			ElMessage({
				message: `表单有${Object.keys(fields).length}处验证失败，请修改后再提交`,
				type: "error",
			});
		}
	});
};

// 产品包列表
const resSysMrsPackageApi = async () => {
	await SysMrsPackageApi().queryAll().then((res) => {
		if (res.data.resCode === '0000')
			state.SysMrsPackageList = res.data.result ?? [];
	});
}

// 用户列表
const resSysMrsUserApi = async () => {
	await SysMrsUserApi().queryAll().then((res) => {
		if (res.data.resCode === '0000')
			state.SysMrsUserApiList = res.data.result ?? [];
	});
}

// 导出对象 将属性或者函数暴露给父组件
defineExpose({ openDialog });
</script>
